package logic

import (
	"context"
	"fmt"
	"gorm.io/gorm"
	"hotel/model/mysql"

	"hotel/hotelrpc/hotelrpc"
	"hotel/hotelrpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserUpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUserUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserUpdateLogic {
	return &UserUpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UserUpdateLogic) UserUpdate(in *hotelrpc.UserUpdateRequest) (*hotelrpc.UserUpdateResponse, error) {
	// todo: add your logic here and delete this line
	var user mysql.User
	err := mysql.DB.Debug().Where("id=?", in.Id).First(&user).Error
	if err == nil {
		return nil, fmt.Errorf("该用户不存在")
	}
	m := mysql.User{
		Model:    gorm.Model{ID: uint(in.Id)},
		Mobile:   in.Mobile,
		Password: in.Password,
		Img:      in.Img,
		Name:     in.Name,
		Sex:      in.Sex,
		Intro:    in.Intro,
	}
	mysql.DB.Debug().Model(&m).Updates(&m)
	return &hotelrpc.UserUpdateResponse{Id: int64(user.ID)}, nil
}
